package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketIssue;
import com.powernobug.bean.model.MarketIssueExample;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.data.IssueCreateData;
import com.powernobug.bean.vo.param.IssueCreateVo;
import com.powernobug.bean.vo.param.IssueListParam;
import com.powernobug.mapper.MarketIssueMapper;
import com.powernobug.service.IssueService;
import com.powernobug.util.MyBatisUtil;
import org.apache.commons.beanutils.BeanUtils;
//import org.apache.commons.beanutils.PropertyUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
/*

 */
public class IssueServiceImpl implements IssueService {
    @Override
    public PageVo list(IssueListParam issueListParam) {
        String sort = issueListParam.getSort();
        String order = issueListParam.getOrder();
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {

            MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);
            MarketIssueExample example = new MarketIssueExample();

            // 排序
            example.setOrderByClause(sort + " " + order);
            issueMapper.selectByExample(example);

            // 分页
            Integer page = issueListParam.getPage();
            Integer limit = issueListParam.getLimit();
            PageHelper.startPage(page, limit);

            List<MarketIssue> marketIssues = issueMapper.selectByExample(example);

            return PageVo.list(marketIssues);
        }
    }

    @Override
    public IssueCreateData create(IssueCreateVo issueCreateVo) {

        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);

            MarketIssue marketIssue = new MarketIssue();

            String answer = issueCreateVo.getAnswer();
            String question = issueCreateVo.getQuestion();

            // 有效判断
            boolean isEffective = answer != null && question != null
                    && !answer.isEmpty() && !question.isEmpty();
            String pattern = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);

            if (isEffective) {
                marketIssue.setAnswer(answer);
                marketIssue.setQuestion(question);

                Date current = new Date();
                marketIssue.setAddTime(current);
                marketIssue.setUpdateTime(current);

                marketIssue.setDeleted(false);

                mapper.insertSelective(marketIssue); // 传入数据库
                sqlSession.commit(); // 事务提交
            }

            // 返回Data
            IssueCreateData data = new IssueCreateData();
            data.setId(marketIssue.getId());
            data.setAnswer(marketIssue.getAnswer());
            data.setQuestion(marketIssue.getQuestion());
            data.setUpdateTime(sdf.format(marketIssue.getUpdateTime()));
            data.setAddTime(sdf.format(marketIssue.getAddTime()));
            return data;

        }
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);
            mapper.deleteByPrimaryKey(marketIssue.getId());
            sqlSession.commit();
        }
    }

    @Override
    public void deleteMap(Map deleteInfoMap) {
        Integer id = (Integer) deleteInfoMap.get("id");
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);

            issueMapper.deleteByPrimaryKey(id);

            sqlSession.commit();
        }
    }

    @Override
    public void update(MarketIssue marketIssue) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketIssueMapper mapper = sqlSession.getMapper(MarketIssueMapper.class);

            mapper.updateByPrimaryKey(marketIssue);

            sqlSession.commit();
        }
    }

    @Override
    public MarketIssue updateMap(MarketIssue marketIssue) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketIssueMapper issueMapper = sqlSession.getMapper(MarketIssueMapper.class);

//            MarketIssue marketIssue = new MarketIssue();
//            BeanUtils.copyProperties(marketIssue, updateInfoMap);
//            PropertyUtils.copyProperties(marketIssue,updateInfoMap);

//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String addTimeStr = (String) updateInfoMap.get("addTime");
//            System.out.println("addTimeStr = " + addTimeStr);
//            Date addTime = dateFormat.parse(addTimeStr);
//            Date currentTime = new Date();
//            marketIssue.setAddTime(addTime);
//            marketIssue.setUpdateTime(currentTime);
            
            int affectedRows = issueMapper.updateByPrimaryKey(marketIssue);
            System.out.println("affectedRows = " + affectedRows);

            sqlSession.commit();
            return marketIssue;
        }
    }
}
